package com.example.robotwatcher;

import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.robotwatcher.backend.DataCollector;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MainFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MainFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    VideoSurfaceView showVideo;
    DataCollector collector;
    static Handler mhandler;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public MainFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MainFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MainFragment newInstance(String param1, String param2) {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        TextView Main = view.findViewById(R.id.textView11);
        Main.setText("FPS");
        EditText Main1 = view.findViewById(R.id.Edittext3);
        Main1.setText("");
        TextView Time = view.findViewById(R.id.textView12);
        Time.setText("时间");
        EditText Time1 = view.findViewById(R.id.Edittext4);
        Time1.setText("");
        TextView Battery = view.findViewById(R.id.textView13);
        Battery.setText("剩余电量");
        EditText Battery1 = view.findViewById(R.id.Edittext5);
        Battery1.setText("");
        TextView Voltage = view.findViewById(R.id.textView14);
        Voltage.setText("超级电容电压");
        EditText Voltage1 = view.findViewById(R.id.Edittext6);
        Voltage1.setText("");
        TextView Power = view.findViewById(R.id.textView15);
        Power.setText("当前实时功率");
        EditText Power1 = view.findViewById(R.id.Edittext7);
        Power1.setText("");
        TextView Speed = view.findViewById(R.id.textView16);
        Speed.setText("当前速度");
        EditText Speed1 = view.findViewById(R.id.Edittext8);
        Speed1.setText("");
        TextView Imaddress = view.findViewById(R.id.textView17);
        Imaddress.setText("目检图像坐标");
        EditText Imaddress1 = view.findViewById(R.id.Edittext9);
        Imaddress1.setText("");
        TextView Woraddress = view.findViewById(R.id.textView18);
        Woraddress.setText("目标世界地址");
        EditText Woraddress1= view.findViewById(R.id.Edittext10);
        Woraddress1.setText("");
        TextView Feedback = view.findViewById(R.id.textView19);
        Feedback.setText("当前给定反馈误差");
        EditText Feedback1 = view.findViewById(R.id.Edittext11);
        Feedback1.setText("");
        TextView Fc = view.findViewById(R.id.textView20);
        Fc.setText("发弹量");
        EditText Fc1 = view.findViewById(R.id.Edittext12);
        Fc1.setText("");
        TextView Fspeed = view.findViewById(R.id.textView21);
        Fspeed.setText("弹丸速度");
        EditText Fspeed1 = view.findViewById(R.id.Edittext13);
        Fspeed1.setText("");


        SharedPreferences dataset = PreferenceManager.getDefaultSharedPreferences(inflater.getContext());

        //Todo 视频配置
        showVideo = view.findViewById(R.id.surfaceView);
        showVideo.setIPAddress(dataset.getAll().get("defult").toString());
        showVideo.Start();

        // Todo 网络通信

        mhandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {

                try {
                    JSONObject json = (JSONObject) msg.obj;
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Main1.setText(new String(String.valueOf(showVideo.FPS)));
                    Time1.setText(df.format(new Date()));
                    Battery1.setText(json.getString("battery"));
                    Voltage1.setText(json.getString("voltage")+"V");
                    Power1.setText(json.getString("w")+"W");
                    Speed1.setText(json.getString("speed")+"m/s");
                    Imaddress1.setText(json.getString("target_position"));
                    Woraddress1.setText(json.getString("world_position"));
                    Fc1.setText(json.getString("res_but")+"颗");
                    Feedback1.setText(json.getString("delta")+"度");
                    Fspeed1.setText(json.getString("but_speed")+"m/s");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return false;
            }
        });
        collector= new DataCollector(mhandler);
        collector.setTargetUrl(dataset.getAll().get("defult").toString());
        new Thread(new Runnable() {
            @Override
            public void run() {
                collector.getStateInfo();
            }
        }).start();

        Button button = view.findViewById(R.id.button6);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showVideo.RetryOnline();
            }
        });
        return view;

    }

}